
export default class CanvasOperator {
    constructor(canvas) {
        this.canvas = canvas;
    }

    /* ---------- RAF 封装 ---------- */
    raf(cb) {
        // #ifdef MP-WEIXIN || MP-ALIPAY
        return this.canvas.requestAnimationFrame(cb);
        // #endif
        // #ifdef H5
        return window.requestAnimationFrame(cb);
        // #endif
    }
    cancelRaf(id) {
        // #ifdef MP-WEIXIN || MP-ALIPAY
        return this.canvas.cancelAnimationFrame(id);
        // #endif
        // #ifdef H5
        return window.cancelAnimationFrame(id);
        // #endif
    }

    /* ---------- 图片加载 ---------- */
    async loadImage(filePath) {
        let img;
        if (typeof this.canvas.createImage === 'function') {
            img = this.canvas.createImage();
        }
        if (typeof Image !== 'undefined') {
            img = new Image();
        }
        if (typeof wx !== 'undefined') {
            filePath = `/${filePath}`; //微信环境下添加前缀/
        }
        img.crossOrigin = 'anonymous';
        return new Promise((resolve, reject) => {
			img.src = filePath;
            img.onload = () => resolve(img);
            img.onerror = () => { console.error('[loadImage] 图片加载失败:', filePath,'请检查图片路径'); reject };
        });
    }

}